//
//  contact.c
//  Contact
//
//  Created by Daniel on 2024/6/13.
//

#include "contact.h"

void InitContact(Contact* pc)
{
    assert(pc);
    pc->count = 0;
    memset(pc->data,0,sizeof(pc->data));
}

void AddContact(Contact* pc)
{
    assert(pc);
    if(pc->count == MAX)
    {
        printf("通讯录已满\n");
        return;
    }
    printf("请输入名字:>");
    scanf("%s",pc->data[pc->count].name);
    printf("请输入年龄:>");
    scanf("%d",&pc->data[pc->count].age);
    printf("请输入性别:>");
    scanf("%s",pc->data[pc->count].gender);
    printf("请输入电话:>");
    scanf("%s",pc->data[pc->count].tel);
    printf("请输入地址:>");
    scanf("%s",pc->data[pc->count].address);
    pc->count++;
    printf("增加成功\n");
}

void ShowContact(const Contact* pc)
{
    assert(pc);
    printf("%-20s\t%-3s\t%-5s\t%-12s\t%-30s\n","Name","Age","Gender","Tel","Address");
    for(int i = 0; i < pc->count; i++)
    {
        printf("%-20s\t%-2d\t%-5s\t%-12s\t%-30s\n",
               pc->data[i].name,
               pc->data[i].age,
               pc->data[i].gender,
               pc->data[i].tel,
               pc->data[i].address);
    }
}

static int FindByName(Contact* pc,char name[])
{
    assert(pc);
    for (int i = 0; i < pc->count; i++)
    {
        if(strcmp(pc->data[i].name,name) == 0)
        {
            return i;
        }
    }
    return  -1;
}

void DelContact(Contact* pc)
{
    assert(pc);
    char name[MAX_NAME] = {0};
    printf("请出入要删除的人的名字:>");
    scanf("%s",name);
    int pos = FindByName(pc,name);//find
    if(pos == -1)
    {
        printf("要删除的人不存在\n");
        return;
    }
    //delete
    for(int i = pos; i < pc->count-1; i++)
    {
        pc->data[i] = pc->data[i+1];
    }
    pc->count--;
    printf("删除成功");
}

void SearchContact(Contact* pc)
{
    assert(pc);
    char name[MAX_NAME] = {0};
    printf("请输入要查找的名字:>");
    scanf("%s",name);
    //    check
    int pos = FindByName(pc,name);
    if(pos == -1)
    {
        printf("此用户不存在\n");
        return;
    }
    printf("%-20s\t%-3s\t%-5s\t%-12s\t%-30s\n","Name","Age","Gender","Tel","Address");
    printf("%-20s\t%-2d\t%-5s\t%-12s\t%-30s\n",
           pc->data[pos].name,
           pc->data[pos].age,
           pc->data[pos].gender,
           pc->data[pos].tel,
           pc->data[pos].address);
}

void ModifyContact(Contact* pc)
{
    assert(pc);
    char name[MAX_NAME] = {0};
    printf("请输入要修改的人的名字:>");
    scanf("%s",name);
    int pos = FindByName(pc,name);
    if(pos == -1)
    {
        printf("此用户不存在\n");
        return;
    }
    int input = 0;
    printf("1. 名字\t2. 年龄\t3. 性别\t4. 电话\t5. 地址\n");
    printf("请选择要修改的信息:>");
    scanf("%d",&input);
    switch(input)
    {
        case 1:
            printf("更改名字为:>");
            scanf("%s",pc->data[pos].name);
            break;
        case 2:
            printf("更改年龄为:>");
            scanf("%d",&pc->data[pos].age);
            break;
        case 3:
            printf("更改性别为:>");
            scanf("%s",pc->data[pos].gender);
            break;
        case 4:
            printf("更改电话为:>");
            scanf("%s",pc->data[pos].tel);
            break;
        case 5:
            printf("更改地址为:>");
            scanf("%s",pc->data[pos].address);
            break;
        default:
            printf("只能更改以上信息");
            break;
    }
    printf("修改成功\n");
    printf("%-20s\t%-3s\t%-5s\t%-12s\t%-30s\n","Name","Age","Gender","Tel","Address");
    printf("%-20s\t%-2d\t%-5s\t%-12s\t%-30s\n",
           pc->data[pos].name,
           pc->data[pos].age,
           pc->data[pos].gender,
           pc->data[pos].tel,
           pc->data[pos].address);
}

int cmp_peo_by_name(const void* e1,const void* e2)
{
    return strcmp(((PeoInfo*)e1)->name,((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
    assert(pc);
    qsort(pc->data,pc->count,sizeof(PeoInfo),cmp_peo_by_name);
    printf("排序成功");
}
